/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RejoindreCreerCanal_GUI.java
 *
 * Created on Mar 20, 2009, 6:22:24 PM
 */

package client;

import java.awt.Color;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author scriptoff
 */
public class RejoindreCreerCanal_GUI extends javax.swing.JDialog {

    Client cl ;
    

    /** Creates new form RejoindreCreerCanal_GUI */
    public RejoindreCreerCanal_GUI(java.awt.Frame parent, boolean modal , Client cl) {
        super(parent, modal);
        this.cl = cl ;
        initComponents();
        this.setLocationRelativeTo(this.getParent());
        setVisible(true);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        canal_textfield = new javax.swing.JTextField();
        mdp_textfield = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        creer_button = new javax.swing.JButton();
        annuler_button = new javax.swing.JButton();
        canal_notify_label = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Rejoindre / Créer un canal");

        mdp_textfield.setEnabled(false);

        jLabel1.setText("Nom du Canal");

        jCheckBox1.setText("Mot de Passe");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        creer_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/client/icons/tick.png"))); // NOI18N
        creer_button.setText("Rejoindre / Creer");
        creer_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                creer_buttonActionPerformed(evt);
            }
        });

        annuler_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/client/icons/cancel.png"))); // NOI18N
        annuler_button.setText("Annuler");
        annuler_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                annuler_buttonActionPerformed(evt);
            }
        });

        canal_notify_label.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                                .addGap(307, 307, 307))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                                .addGap(18, 18, 18)
                                .addComponent(canal_textfield, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE))
                            .addComponent(canal_notify_label, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jCheckBox1)
                                .addGap(27, 27, 27)
                                .addComponent(mdp_textfield, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(148, 148, 148)
                        .addComponent(annuler_button, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(creer_button, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(canal_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(7, 7, 7)
                .addComponent(canal_notify_label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mdp_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox1))
                .addGap(12, 12, 12)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(creer_button)
                    .addComponent(annuler_button))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        if ( !mdp_textfield.isEnabled() )
        {
            mdp_textfield.setEnabled(true);
        }
        else
        {
            mdp_textfield.setEnabled(false);
            mdp_textfield.setText("");
        }
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    private void annuler_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_annuler_buttonActionPerformed
        this.dispose();
}//GEN-LAST:event_annuler_buttonActionPerformed

    private void creer_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_creer_buttonActionPerformed

        String nomCanal = canal_textfield.getText();
        String passwordCanal = new String("Anonymous") ;
        if (nomCanal.isEmpty()) {
            canal_notify_label.setForeground(Color.red);
            canal_notify_label.setText("Veuillez saisir un nom de canal valide.");
        } else {
            if ( mdp_textfield.isEnabled() )
                passwordCanal =  mdp_textfield.getText() ;
            try {
                cl.traiterRejoindreCanal(nomCanal, passwordCanal);
            } catch (Exception ex) {
                new TraiteException(ex, "Je sais pas");
            }
            this.dispose();
        }
}//GEN-LAST:event_creer_buttonActionPerformed

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    RejoindreCreerCanal_GUI dialog = new RejoindreCreerCanal_GUI(new javax.swing.JFrame(), true);
    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent e) {
    System.exit(0);
    }
    });
    dialog.setVisible(true);
    }
    });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton annuler_button;
    private javax.swing.JLabel canal_notify_label;
    private javax.swing.JTextField canal_textfield;
    private javax.swing.JButton creer_button;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField mdp_textfield;
    // End of variables declaration//GEN-END:variables

}
